package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleInfoService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleInfoDto;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class ApArticleInfoServiceImpl implements ApArticleInfoService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleInfoDto dto) {

        // 1.检验参数    "isfollow": true, "islike": true,"isunlike": false,"iscollection": true

        if (dto == null || dto.getArticleId() == null || dto.getAuthorId() == null) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        Boolean  isfollow = false, islike = false, isunlike = false, iscollection = false;


        // 2.获取行为实体
        if (user != null) {
            ApBehaviorEntry apBehaviorEntry = this.apBehaviorEntryService.findByUserIdOrEquipmentId(user.getId(), dto.getEquipmentId());
            if (apBehaviorEntry == null) {
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "获取实体信息失败");
            }

            Query query = Query.query(Criteria.where("entryId").is(apBehaviorEntry.getId()).and("articleId").is(dto.getArticleId()));

            // 是否不喜欢
            ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if (unlikesBehavior != null) {
                isunlike = true;
            }
            // 是否喜欢
            ApLikesBehavior likesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (likesBehavior != null) {
                islike = true;
            }
            // 是否收藏
            ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
            if (apCollection != null) {
                iscollection = true;
            }

            // 是否关注
            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FOLLOW_LIST + user.getId(), dto.getAuthorApUserId().toString());
            if (score != null) {
                isfollow = true;
            }
        }
        // 放入map返回
        Map<String, Object> map = new HashMap<>();
        map.put("iscollection",iscollection);
        map.put("isfollow",isfollow);
        map.put("isunlike",isunlike);
        map.put("islike",islike);

        return ResponseResult.okResult(map);
    }
}
